package com.lune.job.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.Train;
import com.lune.job.service.IResumeService;
import com.lune.job.service.ITrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/train")
@Api(tags = "培训经历管理")
public class TrainController {
    @Autowired
    private ITrainService trainService;

    @Autowired
    private IResumeService resumeService;

    @PostMapping("/")
    @ApiOperation(value = "添加培训经历")
    public RespBean create(@RequestBody Train train) {
        if (trainService.save(train)) {
            return RespBean.success("添加培训经历成功");
        } else {
            return RespBean.error("添加培训经历失败");
        }
    }

    @ApiOperation(value = "删除单个培训经历")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable Integer id) {
        if (trainService.removeById(id)) {
            return RespBean.success("删除培训经历成功");
        }
        return RespBean.error("删除培训经历失败");
    }

    @ApiOperation(value = "批量删除培训经历")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids) {
        if (trainService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除培训经历成功");
        } else {
            return RespBean.error("批量删除培训经历失败");
        }
    }

    @ApiOperation(value = "修改培训经历")
    @PutMapping("/")
    public RespBean update(@RequestBody Train train) {
        if (trainService.updateById(train)) {
            return RespBean.success("修改培训经历成功");
        } else {
            return RespBean.error("修改培训经历失败");
        }
    }

    @ApiOperation(value = "根据id查询培训经历")
    @GetMapping("/detail")
    public Train detail(Integer id) {
        return trainService.getById(id);
    }

    @ApiOperation(value = "分页查询培训经历")
    @PostMapping("/page")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestBody Train train) {
        IPage<Train> trainIPage = trainService.queryPage(currentPage, size, train);
        trainIPage.getRecords().forEach(item -> {
            item.setResume(resumeService.getById(item.getResume_id()));
        });
        return new ResPageBean(trainIPage.getTotal(), trainIPage.getPages(), trainIPage.getRecords());
    }


    @ApiOperation(value = "条件查询培训经历")
    @PostMapping("/query")
    public List<Train> queryDict(
            @RequestBody Train train) {
        List<Train> trains = trainService.queryByCon(train);
        return trains;
    }
}
